package com.emip.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.emip.bean.AreaBean;
import com.emip.dao.AreaDAO;
import com.emip.dao.BaseDAO;

@Repository("areaDAO")
public class AreaDAOImpl extends BaseDAO implements AreaDAO {

	public List<AreaBean> getAll() {
		List<AreaBean> result = this.getHibernateTemplate().find("from AreaBean m order by ID");
		return result;
	}
	
	public int add(AreaBean entity) {
		Integer id = (Integer)this.getHibernateTemplate().save(entity);
		return id.intValue();
	}

	public AreaBean findByID(Integer ID) {
		AreaBean bean = this.getHibernateTemplate().get(AreaBean.class, ID);
		return bean;
	}

	public void remove(AreaBean entity) {
		this.getHibernateTemplate().delete(entity);
	}

	public void update(AreaBean entity) {
		this.getHibernateTemplate().update(entity);
	}

	public Boolean findExist(AreaBean entity) {
		Object[] params = new Object[] {entity.getAreaID()};
		List<AreaBean> result = this.getHibernateTemplate().find("from AreaBean m where areaID=? order by ID", params);
		if (result.size()==0) {
			return false;
		}
		else {
			//是否存在的那条记录的areaID和自己是相同的，如果是的话，那么其实是updata其他字段的一种操作，也是合法操作
			if (entity.getId()==0) {  //新增的情况
				return true;
			} else {
				AreaBean eBean = this.getHibernateTemplate().get(AreaBean.class, entity.getId());
				if (entity.getAreaID().equals(eBean.getAreaID())) {
					return false;
				} else {
					return true;
				}
			}
		}
	}
}
